<link rel="stylesheet" href="plugin/awesome4/css/font-awesome.min.css"/>
<style type="text/css">
    .ul_one li {float: left;margin: 0px 20px 10px 0px;}
    .goodsCatBox select{
        float: left;
        height: 28px;
        width: 150px;
        display: block;
        margin: 6px 10px;
    }
    .goodsCatBox:after {display:block;clear:both;content:"";visibility:hidden;height:0}
    .goodsCatBox{zoom:1}
</style>
<script type="text/javascript" src="plugin/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
    $(function(){
        //拿到数据
        var formName = 'goodsCat';//提交表单的名字
        var data = <?php echo json_encode($r['goods_all_cat']); ?>    
        var existCat = <?php echo $r['goods_exist_cat']; ?> 
        console.log(data);
        //定义 
        var box       = $('.goodsCatBox');
        var optionTpl = box.find('option').remove().addClass('hasValue');
        var selectTpl = box.find('select').remove();
        var emptyTpl  = $("<option value>所有</option>")
        var diction   = []; //小字典 用于记录children
        var oldDic    = []; //存储上次查询的序列

        //重置
        function reset(){
            box.empty()
        }
        //绑定数据
        function dataBind($option,data){
            var number = diction.length
            diction.push(data.children);
            $option.attr({
                'data-id': data.id,
                'data-name': data.name,
                'data-pid':data.pid,
                'data-point':data.point,
                'data-children':number
            }).html(data.name).val(data.id);
        }
        //填入dom
        function fillIn(data,specialOption){
            console.log(data)
            if(data.length < 1) return;
            var newSelect = selectTpl.clone();
            newSelect.append(emptyTpl.clone())
            for(var i in data){
                var newOption = optionTpl.clone();
                dataBind(newOption,data[i])
                if(data[i].id == specialOption){
                    newOption.attr('selected', true);
                }
                newSelect.append(newOption)
            }
            box.append(newSelect)
            //if(data[0].children.length>0){
            //    fillIn(data[0].children)
            //}
        }
        //清除后面的
        function clear($select){
            $select.nextAll().remove()
        }
        //在最后一个有效地select上加上name
        function addName(){
            var selects = box.find('select').removeAttr('name');
            for(var i=selects.length-1;i>=0;i--){
                if($(selects[i]).val()>0){
                    $(selects[i]).attr('name', formName);
                    break;
                    return false;
                }
            }
        }
        //加载提交之前的选项路径。。我真是醉了
        function getOldPath(data,targetId,callback){
            var index = oldDic.length;
            for(var i in data){
                console.log(data[i])
                oldDic[index] = {
                    'data':data,
                    'id':data[i].id
                }
                oldDic[index]['data'] = data
                if(data[i].id == targetId){
                    var indexPlus = index+1;
                    if(data[i].children.length>0){
                        oldDic[indexPlus] = {
                            'data':data[i].children,
                            'id':'-1',
                        }
                    }
                    console.log('结束时候的数据',oldDic)
                    
                    callback()
                    break;
                    return false;
                }
                if(data[i].children.length > 0){
                    if(!getOldPath(data[i].children,targetId,callback)){
                        break;
                        return false;
                    }
                }else if(i==data.length-1){
                    console.log('end',oldDic)
                    oldDic.pop()
                    console.log('pop',oldDic)
                }
            }
            return true;
        }

        //启动
        reset();
        if(existCat>0){
            console.log('has eist')
            getOldPath(data,existCat,function(){
                console.log('asd',oldDic.length)
                console.log(oldDic)
                if(oldDic.length > 0){
                    for(var i=0;i<oldDic.length;i++){
                        console.log(oldDic[0])
                        fillIn(oldDic[i].data,oldDic[i].id)
                    }
                }
                console.log(oldDic)
                //fillIn(oldDic[i].data.children)    
            })
            
        }else{
            fillIn(data)
        }
        addName()

        //绑定
        box.on('change', 'select', function(event) {
            clear($(this))
            var theOption = $(this).find('option:selected');
            if(theOption.hasClass('hasValue')){
                var number = theOption.attr('data-children');
                fillIn(diction[number])
            }
            addName()
        });
    }) 
</script>
    
<form action="<?php e_page("goods","index");?>" method="get">
    <fieldset>
        <legend></legend>
        <label for="goodsCat">商品分类</label>
        <div class="goodsCatBox">
            <select>
                <option>加载中</option>
            </select>
        </div>
        <label for="goodsLabel">商品标签</label>
        <select name="goodsLabel" id="goodsLabel">
            <option value='' >所有</option>
            <?php foreach($r['label'] as $v){?>
                <option value="<?php echo $v['id']?>" <?php if($r['goodsLabel']==$v['id']) echo 'selected'?> ><?php echo $v['name']?></option>
            <?php }?>
        </select>
        <label for="company">商家</label><br/>
        <input type="text" name="company" id="company" placeholder="请输入商家名称"><br/>
        <label for="goodsName">商品名</label><br/>
        <input type="text" name="goodsName" id="goodsName" placeholder="请输入商品名称">
        <input type="submit" value="搜 索"/><br/>
        <div>
            <ul class="ul_one"><?php
                $up='fa fa-long-arrow-down';
                $down='fa fa-long-arrow-up';
                $i['base_price']=isset($r['base_price']) ? $r['base_price']?$down:$up :$down;
                $a['base_price']=isset($r['base_price'])?!$r['base_price']:0;
                $i['sale']=isset($r['sale'])? $r['sale']?$down:$up :$down;
                $a['sale']=isset($r['sale'])?!$r['sale']:0;
                $i['comment']=isset($r['comment'])?  $r['comment']?$down:$up :$down;
                $a['comment']=isset($r['comment'])?!$r['comment']:0;
                ?>
                <li><a href="<?php e_qus($r['paramRemain']) ?>">综合排序</a></li>
                <li><a href="<?php e_qus($r['paramRemain']+array('shopId'=>$r['shopId'],'goodsCat'=>$r['goodsCat'],'goodsLabel'=>$r['goodsLabel'],'goodsName'=>$r['goodsName'],'orderBy'=>'base_price','asc'=>$a['base_price'])) ?>">
                        价格<i class="<?=$i['base_price']?>"></i></a></li>
                <li><a href="<?php e_qus($r['paramRemain']+array('shopId'=>$r['shopId'],'goodsCat'=>$r['goodsCat'],'goodsLabel'=>$r['goodsLabel'],'goodsName'=>$r['goodsName'],'orderBy'=>'sale','asc'=>$a['sale'])) ?>">
                        销量<i class="<?=$i['sale']?>"></i></a></li>
                <li><a href="<?php e_qus($r['paramRemain']+array('shopId'=>$r['shopId'],'goodsCat'=>$r['goodsCat'],'goodsLabel'=>$r['goodsLabel'],'goodsName'=>$r['goodsName'],'orderBy'=>'comment','asc'=>$a['comment'])) ?>">
                        评论<i class="<?=$i['comment']?>"></i></a></li>
            </ul>
        </div>
    </fieldset>
</form>

<table id="dataTable">
    <thead>
    <tr>
        <th>ID</th>
        <th>所属<br/>分类</th>
        <th>商品<br/>类型</th>
        <th>所属<br/>商家</th>
        <th>商品<br/>名称</th>
        <th>商品<br/>价格</th>
        <th>商品<br/>库存</th>
        <th>销量</th>
        <th>评论数</th>
        <th>修改<br/>时间</th>
        <th>站点<br/>费用</th>
        <th>代购<br/>分成</th>
        <th>平台费用</th>
        <th>分享<br/>分成</th>
        <th>所属<br/>标签</th>
        <th>图片管理</th>
        <th>属性管理</th>
        <th>操作<br/>管理</th>
    </tr>
    </thead>
    <tbody>
    <?php foreach($r['goods_info'] as $v){?>
        <tr>
            <td><?php echo $v['gopen_id']?></td>
            <td><?php echo $v['goods_cat']?></td>
            <td><?php foreach($r['goods_type'] as $k=>$g){?>
                    <?php if($v['type']==$k)echo $g;?>
                <?php }?></td>
<!--            <td>--><?php //if(mb_strlen($v['name'])<13)
//                    echo $v['name'];
//                else
//                    echo mb_substr($v['name'],0,13,"UTF-8"),"..."
//                ?><!--</td>-->
            <td><?php echo $v['shop']['company']?></td>
            <td><?php if(mb_strlen($v['name'])<11)
                    echo $v['name'];
                else{
                    echo mb_substr($v['name'],0,10,"UTF-8");
                echo "<br/>";
                echo mb_substr($v['name'],10,17,"UTF-8");
                echo "...";
                }?></td>
            <td><?php echo "￥",$v['price']['base_price'] ?></td>
            <td><?php echo $v['stock']?><?php echo $v['unit']?></td>
            <td><?php echo $v['sale']?><?php echo $v['unit']?></td>
            <td><?php echo $v['comment']?>条</td>
            <td><?php echo $v['create_time']?></td>
            <td><?php echo "￥",$v['service_fee']?></td>
            <td><?php echo "￥",$v['commission']?></td>
            <td><?php echo "￥",$v['platform_fee']?></td>
            <td><?php echo "￥",$v['bonus']?></td>
            <td>
                <?php foreach($v['label'] as $k=>$l){?>
                    <?php echo $l['name'];?>
                    <?php if($k%3==2) echo "<br>"; ?>
                <?php }?>
            </td>
            <td><a href='<?php e_action("pic","gopen_id={$v['gopen_id']}");?>'>查看</a></td>
            <td><a href='<?php e_action("nature","id={$v['id']}");?>'>查看</a></td>
            <td><a href='<?php e_action("modify","id={$v['id']}");?>'>修改</a>/<a href='<?php e_action("delete","id={$v['id']}");?>' onclick="return confirm('确定删除吗？');">删除</a></td>
        </tr>
    <?php } ?>
    </tbody>
</table>
<?php
/** @var Page $page */
$page=$r['pages'];
?>
<?php echo $page->getPageHtml(); ?>

